package ModelLayer;
import java.util.ArrayList;

/**
 * Vestbjerg project
 * @GroupB2 (UCN - Computer Science DMAJ0914) 
 */
public class Lease
{
    // instance variables
    private String id;
    private String leaseDate;
    private String returnDate;
    private int period;
    private String state;
    private ArrayList<Unit> unitList;
    private int discount;
    private Customer customer;
    
    private static int maxDiscount = Purchase.getMaxDiscount();

    /**
     * Constructor for objects of class Lease
     */
    public Lease(String id, String leaseDate, String returnDate, int period, String state, int discount)
    {
        // initialise instance variables
        this.id = id;
        this.leaseDate = leaseDate;
        this.returnDate = returnDate;
        this.period = period;
        this.state = state;
        this.discount = discount;

        unitList = new ArrayList<Unit>();
    }

    //Constructor for new sale menu ( only takes id , leaseDate) 
    public Lease(String id, String leaseDate) 
    {
        this.id = id;
        this.leaseDate = leaseDate;
        period = 0;
        returnDate = null;
        state = "active";
        discount = 0;
        
        unitList = new ArrayList<Unit>();
    }

    public void setId(String id)
    {
        this.id = id;

    }

    public void setLeaseDate(String leaseDate)
    {
        this.leaseDate = leaseDate;
    }

    public void setReturnDate(String returnDate)
    {
        this.returnDate = returnDate;
    }

    public void setPeriod(int period)
    {
        this.period = period;
    }

    public void setState(String state)
    {
        this.state = state;
    }

    public void setDiscount(int newDiscount) 
    {
        if(newDiscount <= maxDiscount) {
            discount = newDiscount;
        } 
        else {
            discount = maxDiscount;
        }
    }
    
    public void setCustomer(Customer newCustomer) 
    {
        customer = newCustomer;
        if(customer != null) 
        {
            setDiscount(10);
        }
    }

    public void addUnit(Unit unit) 
    {
        if(!unitList.contains(unit)) {
            unitList.add(unit);
        }
    }
    
    public void removeUnit(Unit unit) 
    {
        if(unitList.contains(unit)) 
        {
            unitList.remove(unit);
        }
    }

    public String getId()
    {
        return id;
    }

    public String getLeaseDate()
    {
        return leaseDate;
    }

    public String getReturnDare()
    {
        return returnDate;
    }
    
    public ArrayList<Unit> getUnitList() 
    {
        return unitList;
    }
    
    public Customer getCustomer() 
    {
        return customer;
    }
    
    public int getPeriod()
    {
        return period;
    }

    public String getState()
    {
        return state;
    }

    public int getDiscount()
    {
        return discount;
    }
    
    public static int getMaxDiscount() 
    {
        return maxDiscount;
    }
    
    public double subtotal() 
    {
        double subtotal = 0;
        for(Unit unit : unitList) 
        {
            subtotal += unit.getMachinery().getPrice();
        }
        if (discount > 0 ) 
        {
            subtotal -= subtotal * discount/100;
        }
        return subtotal;
    }
}
